﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.ApllicationDto;

namespace Zocono.WCS.Domain.JobDomain
{
    public class JobInfoService : IJobInfoService
    {
        private readonly ILogUtil _logUtil;
        private readonly ISnowflake _Snowflake;
        private readonly IJobInfoRepository _jobInfoRepository;

        public JobInfoService(IJobInfoRepository jobInfoRepository
            , ILogUtil logUtil
            , ISnowflake snowflake)
        {
            _jobInfoRepository = jobInfoRepository;
            _logUtil = logUtil;
            _Snowflake = snowflake;
        }

        public async Task<List<JobInfo>> GetJobInfos(string programeCode)
        {
            var jobInfos = await _jobInfoRepository.GetListAsync(j => j.ProgrameCode == programeCode);
            return jobInfos;
        }
        public async Task<JobInfo> GetJobInfo(string programeCode, string jobName)
        {
            var jobInfo = await _jobInfoRepository.GetFirstAsync(j => j.ProgrameCode == programeCode && j.JobName == jobName);
            return jobInfo;
        }

        public async Task<PageDto<JobInfo>> GetPagedJobInfoAsync(QueryJobInfoDto queryJobInfo)
        {
            var exp = Expressionable.Create<JobInfo>();
            exp.AndIF(!string.IsNullOrEmpty(queryJobInfo.ProgrameCode), it => it.ProgrameCode.Contains(queryJobInfo.ProgrameCode));
            exp.AndIF(!string.IsNullOrEmpty(queryJobInfo.JobName), it => it.JobName.Contains(queryJobInfo.JobName));

            PageDto<JobInfo> pagedData = new PageDto<JobInfo>();
            RefAsync<int> totalCount = 0;

            var result = await _jobInfoRepository.AsSugarClient().Queryable<JobInfo>()
                .Where(exp.ToExpression())
                .ToPageListAsync(queryJobInfo.PageIndex, queryJobInfo.PageSize, totalCount);

            pagedData.Result = result;
            pagedData.Total = totalCount;

            return pagedData;
        }

        public async Task<List<JobInfo>> GetJobInfoListAsync()
        {
            var jobInfoList = await _jobInfoRepository.GetListAsync();
            return jobInfoList;
        }

        public async Task<bool> CreateJobInfoAsync(JobInfo jobInfo)
        {
            jobInfo.ID = _Snowflake.GetId();
            bool isOk = await _jobInfoRepository.InsertAsync(jobInfo);

            return isOk;

        }

        public async Task<bool> ModifyJobInfoAsync(JobInfo jobInfo)
        {
            bool isOk = await _jobInfoRepository.UpdateAsync(
                p => new JobInfo
                {
                    ProgrameCode = jobInfo.ProgrameCode,
                    JobName = jobInfo.JobName
                },
                p => p.ID == jobInfo.ID);

            return isOk;
        }

        public async Task<bool> DeleteJobInfoAsync(List<JobInfo> jobInfoList)
        {
            try
            {
                _jobInfoRepository.AsTenant().BeginTran();

                foreach (var item in jobInfoList)
                {
                    await _jobInfoRepository.DeleteByIdAsync(item.ID);
                }

                _jobInfoRepository.AsTenant().CommitTran();

                return true;
            }
            catch (Exception ex)
            {
                _jobInfoRepository.AsTenant().RollbackTran();

                throw new Exception(ex.Message);
            }
        }

        public async Task<JobInfo> GetJobInfoByIdAsync(long id)
        {
            var jobInfo = await _jobInfoRepository.GetByIdAsync(id);
            return jobInfo;
        }

        public async Task<bool> DelectJobInfoByIdAsync(long id)
        {
            var res = await _jobInfoRepository.DeleteByIdAsync(id);
            return res;
        }

        
        public async Task<JobInfo> GetJobInfoByJobNameAsync(string jobName)
        {
            var jobInfo = await _jobInfoRepository.GetFirstAsync(j => j.JobName == jobName);
            return jobInfo;
        }
    }
}
